﻿Imports Runa.Resources.ResourceManager
Imports Runa.Core.GamePackage
Imports System.Runtime.InteropServices
Imports Windows.Foundation.Metadata
Imports System.Reflection
Imports Runa.Common.Helper
Imports System.Runtime.InteropServices.WindowsRuntime

' The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

''' <summary>
''' An empty page that can be used on its own or navigated to within a Frame.
''' </summary>
Public NotInheritable Class GameGeneralPage
    Inherits Page

    Private m_gamePackage As GamePackageBase

    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
    End Sub

    Public Sub New(ByVal g As GamePackageBase)
        InitializeComponent()        
        Me.m_gamePackage = g
        Me.Layout.DataContext = Me.m_gamePackage
    End Sub

    ''' <summary>
    ''' Invoked when this page is about to be displayed in a Frame.
    ''' </summary>
    ''' <param name="e">Event data that describes how this page was reached.  The Parameter
    ''' property is typically used to configure the page.</param>
    Protected Overrides Sub OnNavigatedTo(e As Navigation.NavigationEventArgs)

    End Sub

    Private Sub Btn_Cancel_Click(sender As Object, e As RoutedEventArgs)
        Me.Layout.DataContext = Nothing
        Me.Layout.DataContext = Me.m_gamePackage
    End Sub

    Private Sub Btn_Save_Click(sender As Object, e As RoutedEventArgs)
        Dim t = Me.m_gamePackage.GetType()
        For Each item In Me.Grid_Settings.Children
            If TypeOf item Is TextBox Then
                Dim ctrl = CType(item, TextBox)
                Dim p = t.GetRuntimeProperty(ctrl.Name.GetRight("_"))
                Dim value = ctrl.Text.Trim()
                Dim oldValue = p.GetValue(Me.m_gamePackage)
                p.SetValueEx(Me.m_gamePackage, value, oldValue)
            ElseIf TypeOf item Is ComboBox Then
                Dim ctrl = CType(item, ComboBox)
                Dim p = t.GetRuntimeProperty(ctrl.Name.GetRight("_"))
                Dim value = ctrl.SelectedItem.ToString().Trim()
                Dim oldValue = p.GetValue(Me.m_gamePackage)
                p.SetValueEx(Me.m_gamePackage, value, oldValue)
            ElseIf TypeOf item Is CheckBox Then
                Dim ctrl = CType(item, CheckBox)
                Dim p = t.GetRuntimeProperty(ctrl.Name.GetRight("_"))
                Dim value = ctrl.IsChecked
                Dim oldValue = p.GetValue(Me.m_gamePackage)
                p.SetValueEx(Me.m_gamePackage, value.Value, oldValue)
            End If
        Next
    End Sub

End Class